package com.phpxiu.app.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.CoordinateConverter;
import com.phpxiu.app.api.listener.WindowLayoutListener;
import com.phpxiu.app.config.Constants;
import com.phpxiu.app.config.HttpConfig;
import com.phpxiu.app.kkylive.KKYApp;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.CurLiveInfo;
import com.phpxiu.app.model.GoogleAddress;
import com.phpxiu.app.model.GoogleLocation;
import com.phpxiu.app.model.MySelfInfo;
import com.phpxiu.app.model.Permission;
import com.phpxiu.app.model.ShareContent;
import com.phpxiu.app.model.event.WeiXinEvent;
import com.phpxiu.app.okhttp.OKHttp;
import com.phpxiu.app.okhttp.OKHttpCallback;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.custom.ShareMenu;
import com.phpxiu.app.view.popupwindow.ViewTopPop;
import com.phpxiu.app.view.custom.WindowLayout;
import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * 开播配置界面
 * Created by Administrator on 2016-06-18.
 */
public class PublishLive extends BaseShare implements View.OnClickListener,View.OnTouchListener,WindowLayoutListener,AMapLocationListener {
    public final static String TAG="PublishLive";
    public final static String EXTRA_FRIEND_IDS="extra_friend_ids";
    public final static String EXTRA_LIVE_TYPE="extra_live_type";//直播类型：0普通直播，1私密直播
    public final static long UPDATE_PERIOD=2*60*60*1000;//更新位置周期2小时更新一次
    public final static int REQUEST_PICK_FRIENDS_CODE=0x88;
    private final static int HANDLE_CHECK_LOCATION=0x89;//检测定位
    private final static int HANDLE_UPDATE_LOCATION=0x90;//更新位置定位
    private EditText titleInput;
    private View shareBar;
    private ShareMenu shareSinaSwitch,shareFriendsSwitch,shareWXSwitch,shareQQSwitch,shareZoneSwitch,shareFBSwitch,shareInsSwitch;
    private Button startBtn;
    private ViewTopPop pop;
    private int mScreenWidth=0;
    private Rect mBtnRect=new Rect();
    private ShareContent shareContent=null;
    private String mLocation;
    public  AMapLocationClient mLocationClient = null;
    private String[] friendIds;//私密直播好友id列表
    private ImageView publicIcon,privateIcon;
    private TextView publicTitle,privateTitle;
    private int mLiveMode=0;//0公开模式，1私密模式
    private WindowLayout mainView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.publish_live);
        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP){
            requestMyPermissions(Permission.REQUEST_LOCATION);
        }else {
            handler.sendEmptyMessage(HANDLE_CHECK_LOCATION);
        }
        handler.sendEmptyMessage(HANDLE_CHECK_LOCATION);
        mainView=(WindowLayout)findViewById(R.id.publish_view);
        mainView.addWindowLayoutListener(this);
        String avatar=MySelfInfo.getInstance().getAvatar();
        shareContent=new ShareContent(this);
        shareContent.setTitle(MySelfInfo.getInstance().getNickName());
        shareContent.setSummary(MySelfInfo.getInstance().getNickName());
        shareContent.setImageUrl(avatar);
        shareContent.setTargetUrl(HttpConfig.SHARE_URL + MySelfInfo.getInstance().getId());
        initView();
        KKYUtil.log("PublishLive create use:"+(System.currentTimeMillis()-start)+"ms");
    }

    /**
     *初始化视图
     */
    private void initView(){
        findViewById(R.id.publish_view).setOnTouchListener(this);
        findViewById(R.id.close_btn).setOnClickListener(this);
        titleInput=(EditText)findViewById(R.id.live_title);
        findViewById(R.id.public_mode).setOnClickListener(this);
        findViewById(R.id.private_mode).setOnClickListener(this);
        publicIcon=(ImageView)findViewById(R.id.publish_public_mode_icon);
        publicTitle=(TextView)findViewById(R.id.publish_public_mode_title);
        publicTitle.setSelected(true);
        privateIcon=(ImageView)findViewById(R.id.publish_private_mode_icon);
        privateTitle=(TextView)findViewById(R.id.publish_private_mode_title);
        privateTitle.setSelected(false);
        initShareBar();
        startBtn.setOnClickListener(this);
        pop=new ViewTopPop(this);
        mScreenWidth=(int) KKYApp.getInstance().screenWidth;
    }

    /**
     * 初始化分享工具栏
     */
    private void initShareBar(){
        shareBar=findViewById(R.id.share_bar);
        shareSinaSwitch=(ShareMenu)findViewById(R.id.open_share_to_sina_btn);
        shareSinaSwitch.setResOn(R.mipmap.open_share_to_sina_on);
        shareSinaSwitch.setResOff(R.mipmap.open_share_to_sina_off);
        shareSinaSwitch.setMsgOff(getString(R.string.ui_sina_off));
        shareSinaSwitch.setMsgOn(getString(R.string.ui_sina_on));
        shareSinaSwitch.setOnClickListener(this);

        shareFriendsSwitch=(ShareMenu)findViewById(R.id.open_share_to_friends_btn);
        shareFriendsSwitch.setResOn(R.mipmap.open_share_to_friends_on);
        shareFriendsSwitch.setResOff(R.mipmap.open_share_to_friends_off);
        shareFriendsSwitch.setMsgOff(getString(R.string.ui_friends_off));
        shareFriendsSwitch.setMsgOn(getString(R.string.ui_friends_on));
        shareFriendsSwitch.setOnClickListener(this);

        shareWXSwitch=(ShareMenu)findViewById(R.id.open_share_to_wx_btn);
        shareWXSwitch.setResOn(R.mipmap.open_share_to_wx_on);
        shareWXSwitch.setResOff(R.mipmap.open_share_to_wx_off);
        shareWXSwitch.setMsgOff(getString(R.string.ui_wx_off));
        shareWXSwitch.setMsgOn(getString(R.string.ui_wx_on));
        shareWXSwitch.setOnClickListener(this);

        shareQQSwitch=(ShareMenu)findViewById(R.id.open_share_to_qq_btn);
        shareQQSwitch.setResOn(R.mipmap.open_share_to_qq_on);
        shareQQSwitch.setResOff(R.mipmap.open_share_to_qq_off);
        shareQQSwitch.setMsgOff(getString(R.string.ui_qq_off));
        shareQQSwitch.setMsgOn(getString(R.string.ui_qq_on));
        shareQQSwitch.setOnClickListener(this);

        shareZoneSwitch=(ShareMenu)findViewById(R.id.open_share_to_qq_zone_btn);
        shareZoneSwitch.setResOn(R.mipmap.open_share_to_qq_zone_on);
        shareZoneSwitch.setResOff(R.mipmap.open_share_to_qq_zone_off);
        shareZoneSwitch.setMsgOff(getString(R.string.ui_qz_off));
        shareZoneSwitch.setMsgOn(getString(R.string.ui_qz_on));
        shareZoneSwitch.setOnClickListener(this);

        shareFBSwitch=(ShareMenu)findViewById(R.id.open_share_to_fb_btn);
        shareFBSwitch.setResOn(R.mipmap.open_share_to_fb_on);
        shareFBSwitch.setResOff(R.mipmap.open_share_to_fb_off);
        shareFBSwitch.setMsgOff(getString(R.string.ui_fb_off));
        shareFBSwitch.setMsgOn(getString(R.string.ui_fb_on));
        shareFBSwitch.setOnClickListener(this);

        shareInsSwitch=(ShareMenu)findViewById(R.id.open_share_to_ins_btn);
        shareInsSwitch.setResOn(R.mipmap.open_share_to_ins_on);
        shareInsSwitch.setResOff(R.mipmap.open_share_to_ins_off);
        shareInsSwitch.setMsgOff(getString(R.string.ui_ins_off));
        shareInsSwitch.setMsgOn(getString(R.string.ui_ins_on));
        shareInsSwitch.setOnClickListener(this);

        startBtn=(Button)findViewById(R.id.go_create_av_room);
        startBtn.setOnClickListener(this);
    }

    @Override
    protected void onPermission(int requestCode) {
        handler.sendEmptyMessage(HANDLE_CHECK_LOCATION);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(v.getId()== R.id.publish_view&&event.getAction()==MotionEvent.ACTION_DOWN){
            if(pop!=null&&pop.isShowing()){
                pop.dismiss();
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if(pop!=null&&pop.isShowing()){
            pop.dismiss();
        }
        switch (v.getId()){
            case R.id.close_btn:
                onBackPressed();
                break;
            case R.id.public_mode:
                switchPublishMode(0);
                break;
            case R.id.private_mode:
                gotoPickerFriends();
                break;
            case R.id.open_share_to_sina_btn:
                refreshShareState(v.getId());
                break;
            case R.id.open_share_to_friends_btn:
                refreshShareState(v.getId());
                break;
            case R.id.open_share_to_wx_btn:
                refreshShareState(v.getId());
                break;
            case R.id.open_share_to_qq_btn:
                refreshShareState(v.getId());
                break;
            case R.id.open_share_to_qq_zone_btn:
                refreshShareState(v.getId());
                break;
            case R.id.open_share_to_fb_btn:
                refreshShareState(v.getId());
                break;
            case R.id.open_share_to_ins_btn:
                refreshShareState(v.getId());
                break;
            case R.id.go_create_av_room:
                startShare();
                break;
        }
    }

    @Override
    public void handleMsg(Message msg) {
        super.handleMsg(msg);
        switch (msg.what){
            case HANDLE_CHECK_LOCATION:
                checkLocation();
                break;
            case HANDLE_UPDATE_LOCATION:
                if(msg.obj==null){
                    mLocation=getString(R.string.default_location);
                }else{
                    mLocation=msg.obj.toString();
                    MySelfInfo.getInstance().updateCacheString(this, Constants.USER_LAST_LOCATION, mLocation);
                    MySelfInfo.getInstance().updateCacheLong(this, Constants.USER_LAST_LOCATION_UPDATE_TIME, System.currentTimeMillis());
                }
                CurLiveInfo.setAddress(mLocation);
                break;
            default:
                break;
        }
    }

    /**
     * 检测位置
     */
    private void checkLocation(){
        mLocation=MySelfInfo.getInstance().getLastLocation();
        if(mLocation!=null){
            long lastTime=MySelfInfo.getInstance().getLocationUpdateTime();
            if((System.currentTimeMillis()-lastTime)<UPDATE_PERIOD){//距上次获取地址时间不大于2小时
                CurLiveInfo.setAddress(mLocation);
                return;
            }
        }
        if(mLocationClient==null){
            mLocationClient = new AMapLocationClient(getApplicationContext());
        }
        mLocationClient.setLocationOption(KKYApp.LP);
        mLocationClient.setLocationListener(this);
        mLocationClient.startLocation();
    }

    @Override
    public void onLocationChanged(AMapLocation location) {
        String AMapAddress=null;
        KKYUtil.log("高德定位返回："+location.getErrorCode()+"--"+location.getErrorInfo());
        CoordinateConverter converter  = new CoordinateConverter(this);
        //返回true代表当前位置在大陆、港澳地区，反之不在。
        boolean isInChina = converter.isAMapDataAvailable(location.getLatitude(),location.getLongitude());
        if(isInChina){//在中国用高德定位
            KKYUtil.log("高德当前地理位置：中国境内");
            if (location.getErrorCode() == 0) {//高德定位成功
                String address=location.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
                String country=location.getCountry();//国家信息
                String province=location.getProvince();//省信息
                String city=location.getCity();//城市信息
                KKYUtil.log("定位地址："+address);
                KKYUtil.log("定位国家："+country);
                KKYUtil.log("定位省份："+province);
                KKYUtil.log("定位城市 ："+city);
                if(province!=null){
                    province=province.replace("省","·");
                }
                if(city!=null){
                    city=city.replace("市","");
                }
                AMapAddress=province+city;
                if(KKYUtil.DEBUG){
                    Toast.makeText(this,"高德定位当前位置："+AMapAddress,Toast.LENGTH_LONG).show();
                }
            }else {
                KKYUtil.log("定位失败：" + location.getErrorCode() + "@" + location.getErrorInfo());
            }
            if(AMapAddress!=null){
                updateLocation(AMapAddress);
                return;
            }
        }
        //Geocoding
        String extraParam=location.getLatitude()+","+location.getLongitude()+"&language="+getString(R.string.language);
        KKYUtil.log("Google定位Api调用："+extraParam);
        OKHttp.get(HttpConfig.GOOGLE_GEOCODING + extraParam, TAG, new OKHttpCallback(false) {
            @Override
            public void onSuccess(String result) {
                KKYUtil.log("Google定位Api成功响应："+result);
                String googleAdd=null;
                try {
                    GoogleLocation g = OKHttp.GSON.fromJson(result, GoogleLocation.class);
                    List<GoogleAddress> addressList = g.getResults().get(0).getAddress_components();
                    if (addressList.size() > 0) {
                        googleAdd = addressList.get(0).getLong_name();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                updateLocation(googleAdd);
            }

            @Override
            public void onErr(String msg) {
                KKYUtil.log("Google定位Api失败："+msg);
                updateLocation(null);
            }
        });
    }

    /**
     * 更新位置
     * @param newAddress
     */
    final private void updateLocation(String newAddress){
        Message message=handler.obtainMessage();
        message.what=HANDLE_UPDATE_LOCATION;
        message.obj=newAddress;
        handler.sendMessage(message);
    }

    /**
     * 切换直播模式
     */
    private void switchPublishMode(int mode){
        if(mLiveMode==mode){
            return;
        }
        mLiveMode=mode;
        if(mode==1){//切换前为公开模式
            //切换为私密模式
            if(shareBar.getVisibility()==View.VISIBLE){//私密模式不能做分享操作
                shareBar.setVisibility(View.INVISIBLE);
            }
            publicIcon.setImageResource(R.drawable.publish_public_mode_normal);
            privateIcon.setImageResource(R.drawable.publish_private_mode_selected);
            publicTitle.setSelected(false);
            privateTitle.setSelected(true);
        }else{
            //切换为公开模式
            if(shareBar.getVisibility()!=View.VISIBLE){//公开模式显示分享操作
                shareBar.setVisibility(View.VISIBLE);
            }
            publicIcon.setImageResource(R.drawable.publish_public_mode_selected);
            privateIcon.setImageResource(R.drawable.publish_private_mode_normal);
            publicTitle.setSelected(true);
            privateTitle.setSelected(false);
        }
    }

    /**
     * 刷新分享按钮状态
     * @param id
     */
    public void refreshShareState(int id){
        switch (id){
            case R.id.open_share_to_sina_btn:
                if(!KKYApp.mSinaShareAPI.isWeiboAppInstalled()){
                    Toast.makeText(this, "新浪微博未安装!", Toast.LENGTH_LONG).show();
                    return;
                }
                shareSinaSwitch.getGlobalVisibleRect(mBtnRect);
                if(shareSinaSwitch.isChecked()){
                    pop.showAtLocation(getWindow().getDecorView(),shareSinaSwitch.getMsgOff(),mBtnRect,mScreenWidth);
                }else{
                    pop.showAtLocation(getWindow().getDecorView(),shareSinaSwitch.getMsgOn(),mBtnRect,mScreenWidth);
                }
                shareSinaSwitch.refreshState(!shareSinaSwitch.isChecked());
                shareInsSwitch.refreshState(false);
                shareFBSwitch.refreshState(false);
                shareFriendsSwitch.refreshState(false);
                shareWXSwitch.refreshState(false);
                shareQQSwitch.refreshState(false);
                shareZoneSwitch.refreshState(false);
                break;
            case R.id.open_share_to_friends_btn:
                if(!KKYApp.mWeiXinAPI.isWXAppInstalled()){
                    Toast.makeText(this, "微信未安装!", Toast.LENGTH_LONG).show();
                    return;
                }
                shareFriendsSwitch.getGlobalVisibleRect(mBtnRect);
                if(shareFriendsSwitch.isChecked()){
                    pop.showAtLocation(getWindow().getDecorView(),shareFriendsSwitch.getMsgOff(),mBtnRect,mScreenWidth);
                }else{
                    pop.showAtLocation(getWindow().getDecorView(),shareFriendsSwitch.getMsgOn(),mBtnRect,mScreenWidth);
                }
                shareFriendsSwitch.refreshState(!shareFriendsSwitch.isChecked());
                shareInsSwitch.refreshState(false);
                shareFBSwitch.refreshState(false);
                shareSinaSwitch.refreshState(false);
                shareWXSwitch.refreshState(false);
                shareQQSwitch.refreshState(false);
                shareZoneSwitch.refreshState(false);
                break;
            case R.id.open_share_to_wx_btn:
                if(!KKYApp.mWeiXinAPI.isWXAppInstalled()){
                    Toast.makeText(this, "微信未安装!", Toast.LENGTH_LONG).show();
                    return;
                }
                shareWXSwitch.getGlobalVisibleRect(mBtnRect);
                if(shareWXSwitch.isChecked()){
                    pop.showAtLocation(getWindow().getDecorView(),shareWXSwitch.getMsgOff(),mBtnRect,mScreenWidth);
                }else{
                    pop.showAtLocation(getWindow().getDecorView(),shareWXSwitch.getMsgOn(),mBtnRect,mScreenWidth);
                }
                shareWXSwitch.refreshState(!shareWXSwitch.isChecked());
                shareInsSwitch.refreshState(false);
                shareFBSwitch.refreshState(false);
                shareSinaSwitch.refreshState(false);
                shareFriendsSwitch.refreshState(false);
                shareQQSwitch.refreshState(false);
                shareZoneSwitch.refreshState(false);
                break;
            case R.id.open_share_to_qq_btn:
                shareQQSwitch.getGlobalVisibleRect(mBtnRect);
                if(shareQQSwitch.isChecked()){
                    pop.showAtLocation(getWindow().getDecorView(),shareQQSwitch.getMsgOff(),mBtnRect,mScreenWidth);
                }else{
                    pop.showAtLocation(getWindow().getDecorView(),shareQQSwitch.getMsgOn(),mBtnRect,mScreenWidth);
                }
                shareQQSwitch.refreshState(!shareQQSwitch.isChecked());
                shareInsSwitch.refreshState(false);
                shareFBSwitch.refreshState(false);
                shareSinaSwitch.refreshState(false);
                shareFriendsSwitch.refreshState(false);
                shareWXSwitch.refreshState(false);
                shareZoneSwitch.refreshState(false);
                break;
            case R.id.open_share_to_qq_zone_btn:
                shareZoneSwitch.getGlobalVisibleRect(mBtnRect);
                if(shareZoneSwitch.isChecked()){
                    pop.showAtLocation(getWindow().getDecorView(),shareZoneSwitch.getMsgOff(),mBtnRect,mScreenWidth);
                }else{
                    pop.showAtLocation(getWindow().getDecorView(),shareZoneSwitch.getMsgOn(),mBtnRect,mScreenWidth);
                }
                shareZoneSwitch.refreshState(!shareZoneSwitch.isChecked());
                shareFBSwitch.refreshState(false);
                shareInsSwitch.refreshState(false);
                shareSinaSwitch.refreshState(false);
                shareFriendsSwitch.refreshState(false);
                shareWXSwitch.refreshState(false);
                shareQQSwitch.refreshState(false);
                break;
            case R.id.open_share_to_fb_btn:
                shareFBSwitch.getGlobalVisibleRect(mBtnRect);
                if(shareFBSwitch.isChecked()){
                    pop.showAtLocation(getWindow().getDecorView(),shareFBSwitch.getMsgOff(),mBtnRect,mScreenWidth);
                }else{
                    pop.showAtLocation(getWindow().getDecorView(),shareFBSwitch.getMsgOn(),mBtnRect,mScreenWidth);
                }
                shareFBSwitch.refreshState(!shareFBSwitch.isChecked());
                shareInsSwitch.refreshState(false);
                shareZoneSwitch.refreshState(false);
                shareSinaSwitch.refreshState(false);
                shareFriendsSwitch.refreshState(false);
                shareWXSwitch.refreshState(false);
                shareQQSwitch.refreshState(false);
                break;
            case R.id.open_share_to_ins_btn:
                shareInsSwitch.getGlobalVisibleRect(mBtnRect);
                if(shareInsSwitch.isChecked()){
                    pop.showAtLocation(getWindow().getDecorView(),shareInsSwitch.getMsgOff(),mBtnRect,mScreenWidth);
                }else{
                    pop.showAtLocation(getWindow().getDecorView(),shareInsSwitch.getMsgOn(),mBtnRect,mScreenWidth);
                }
                shareInsSwitch.refreshState(!shareInsSwitch.isChecked());
                shareFBSwitch.refreshState(false);
                shareZoneSwitch.refreshState(false);
                shareSinaSwitch.refreshState(false);
                shareFriendsSwitch.refreshState(false);
                shareWXSwitch.refreshState(false);
                shareQQSwitch.refreshState(false);
                break;
            default:
                break;
        }

    }

    /**
     * 开始直播
     */
    private void startShare(){
        if(mLiveMode==1){//私密直播
            onStartLive();
            return;
        }
        if(shareSinaSwitch.isChecked()){//分享到新浪
            onRequestShareContentTo(PLATFORM_WB, shareContent);
        }else if(shareFriendsSwitch.isChecked()){//朋友圈
            onRequestShareContentTo(PLATFORM_WF, shareContent);
        }else if(shareWXSwitch.isChecked()){//微信好友
            onRequestShareContentTo(PLATFORM_WX, shareContent);
        }else if(shareQQSwitch.isChecked()){//QQ好友
            onRequestShareContentTo(PLATFORM_QQ, shareContent);
        }else if(shareZoneSwitch.isChecked()){//QQ空间
            onRequestShareContentTo(PLATFORM_QZ, shareContent);
        }else if(shareFBSwitch.isChecked()){
            onRequestShareContentTo(PLATFORM_FB, shareContent);
        }else if(shareInsSwitch.isChecked()){
            onRequestShareContentTo(PLATFORM_INS, shareContent);
        }else{
            onStartLive();
        }
    }

    @Override
    protected void onPlatformShareBack(int platform) {
        super.onPlatformShareBack(platform);
        onStartLive();
    }

    /**
     * 前往私密直播好友选择界面
     */
    private void gotoPickerFriends(){
        switchPublishMode(1);
        Intent pickFriend=new Intent(this,FriendsPicker.class);
        startActivityForResult(pickFriend,REQUEST_PICK_FRIENDS_CODE);
    }


    private void onStartLive(){
        Intent intent = new Intent(this, RoomVideoRoom.class);
        intent.putExtra(Constants.ID_STATUS, Constants.HOST);
        String type="0";//默认普通直播
        if(mLiveMode==1){
            if(friendIds!=null&&friendIds.length>0){
                intent.putExtra(EXTRA_FRIEND_IDS,friendIds);
            }
            type="1";//私密直播标志参数
            CurLiveInfo.isPrivate=true;
        }else{
            CurLiveInfo.isPrivate=false;
        }
        intent.putExtra(EXTRA_LIVE_TYPE,type);
        MySelfInfo.getInstance().setIdStatus(Constants.HOST);
        CurLiveInfo.setHostName(MySelfInfo.getInstance().getNickName());
        CurLiveInfo.setTitle(titleInput.getText().toString());
        CurLiveInfo.setHostID(MySelfInfo.getInstance().getId());
        CurLiveInfo.setHostAvator(MySelfInfo.getInstance().getAvatar());
        CurLiveInfo.setRoomNum(MySelfInfo.getInstance().getMyRoomNum());
        startActivity(intent);
        this.finish();
    }

    @Override
    public void onInterceptTouch(MotionEvent ev) {
        hideSoftInput(titleInput);
    }
    /**
     * 关闭键盘
     */
    protected void hideSoftInput(View input) {
        if(input == null) return;
        InputMethodManager imm = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(input.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==REQUEST_PICK_FRIENDS_CODE){
            if(resultCode==RESULT_OK){
                friendIds=data.getStringArrayExtra(EXTRA_FRIEND_IDS);
                if(friendIds!=null&&friendIds.length>0){
                    //onStartLive();//有选择好友，直接开播
                }
            }else{
                friendIds=null;
                KKYUtil.log("取消私密直播");
            }
            return;
        }
        super.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    public void finish() {
        if(shareContent!=null){
            shareContent.release();
            shareContent=null;
        }
        if(mLocationClient!=null){
            mLocationClient.stopLocation();
        }
        super.finish();
    }

    @Override
    protected void onDestroy() {
        if(mLocationClient!=null){
            mLocationClient.stopLocation();
            mLocationClient.onDestroy();
        }
        KKYUtil.log("注销了"+TAG);
        if(shareContent!=null){
            shareContent.release();
            shareContent=null;
        }
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void finalize() throws Throwable {
        Log.e("KKYLog",TAG+"finalize...");
        super.finalize();
        Log.e("KKYLog",TAG+"finalize");
    }

    /**
     * 微信分享完毕
     */
    @Subscribe
    public void onWXShare(WeiXinEvent event){
        if(WeiXinEvent.SHARE==event.getCode()){
            onPlatformShareBack(PLATFORM_WX);
        }
    }

}
